<?php
class AdvertAction extends Action{
	public function _initialize(){
		if(!isset($_SESSION["pid"])){
			if(!isset($_COOKIE["pid"])){
				$this->redirect("partner/Public/login");
			}else{
				$_SESSION["pid"] = $_COOKIE["pid"];
				$_SESSION["name"] = M("Partner")->where("pid = ".$_SESSION["pid"])->find()["name"];
			}
		}
		
		$this->assign("mod","advert");
	}
	public function index(){
		$pid = $_SESSION["pid"];
		/*商家信息*/
		$partner = M("Partner")->where("pid = $pid")->find();
		//显示数据
			//可使用
		$adverts = M("Advert")->where("pid = $pid and is_del = 'n'")->field("count(*)")->find()["count(*)"];
		$onlink_advert  = M("Advert")->where("pid = $pid and is_del = 'n' and status = 1")->field("count(*)")->find()["count(*)"];
			//现在情况
		if($adverts == null) $adverts=0;
		if($onlink_advert == null) $onlink_advert=0;
			//点击情况
			
		
			//广告列表
		//通过的
		$advert_pass = M("Advert")->where("pid = $pid and is_del = 'n' and status = 1")->select();
		
		if($advert_pass == null) $advert_pass = 0;
		//未通过的
		$advert_unpass = M("Advert")->where("pid = $pid and is_del = 'n' and (status = 0 or status = -1)")->select();
		
		
		$this->assign("click_num",$partner["click_num"]);
		$this->assign("click_day",$partner["click_day"]);
		$this->assign("adverts",$adverts);
		$this->assign("onlink_advert",$onlink_advert);
		$this->assign("advert_pass",$advert_pass);
		$this->assign("advert_unpass",$advert_unpass);
		
		
		$this->display("advert_index");
	}
	
	public function addAdvert(){
		$this->display("advert_add");
	}
	
	public function addOK(){
		
		$advert = M("Advert");
		$advert->title = t($_REQUEST["title"]);
		$advert->is_link = t($_REQUEST["is_link"]);
		$advert->link = t($_REQUEST["link"]);
		$advert->content = t($_REQUEST["content"]);
		$advert->is_banner = t($_REQUEST["is_banner"]);
		$advert->pid = $_SESSION["pid"];
		$advert->ctime = time();
		$advert->status = 0;
		if(isset($_FILES["pic"])){
			if($_FILES["pic"]["error"] == 0){
			$upload= new UploadFile();
			$upload->autoSub=true; //开启子目录保存文件
			$upload->maxSize = 2097152;//最大为2m
			$upload->subName=array('date','Y-m-d');//以时间格式生成文件夹
			$upload->exts=array('jpg','jpeg','gif','png','bmp');//可上传的类型
			$upload->savePath='data/advert/pic/';
			$info=$upload->upload();
			
				
			if(!$info){
				$this->error($upload->getError(),'add',3);
			}
			$avatarinfo = $upload->getUploadFileInfo();
			//dump($avatarinfo);exit;
			$advert->pic=$avatarinfo['0']['savepath'].$avatarinfo['0']['savename'];
			}else{
				$data=array("massage"=>"图片上传错误，请重新上传！","url"=>"partner/Advert/add");
				$this->redirect("partner/Public/redirectHtml",$data);
				exit;
			}
		}else{
			$data=array("massage"=>"必须上传图片！","url"=>"partner/Advert/add");
			$this->redirect("partner/Public/redirectHtml",$data);
			exit;
		}
		
		if($advert->add()){
			$data=array("massage"=>"添加成功","url"=>"partner/Advert/index");
			$this->redirect("partner/Public/redirectHtml",$data);
			exit;
		}else{
			$data=array("massage"=>"添加失败","url"=>"partner/Advert/index");
			$this->redirect("partner/Public/redirectHtml",$data);
			exit;
		}
		
	}
	
	//上线
	public function displayAdvert(){
		$aid = $_REQUEST["aid"];
		$AD = M("Advert");
		$advert = $AD->where("aid = $aid")->find();
		
		//判断套餐
		if($advert["is_banner"]){
			//轮播图的
			$data=array("aid"=>$aid);
			$this->redirect("partner/Advert/displayBanner",$data);
			exit;
		}
		// 商家的次数
		$pid = $_SESSION["pid"];
		$partner = M("Partner")->where("pid=$pid")->find();

		if($partner["click_num"]<=0){
			$data=array("massage"=>"你的次数不足够！请充值","url"=>"partner/Advert/index");
			$this->redirect("partner/Public/redirectHtml",$data);
			exit;
		}
		//判断审核状态	
		if($advert["status"]){
			$AD->is_display = 'y';
			if($AD->where("aid = $aid")->save()){
				$data=array("massage"=>"上线成功","url"=>"partner/Advert/index");
				$this->redirect("partner/Public/redirectHtml",$data);
				exit;
			}else{
				$data=array("massage"=>"上线失败","url"=>"partner/Advert/index");
				$this->redirect("partner/Public/redirectHtml",$data);
				exit;
			}
		}else{
			$data=array("massage"=>"该广告未通过审核","url"=>"partner/Advert/index");
			$this->redirect("partner/Public/redirectHtml",$data);
			exit;
		}
	}
	
	public function displayBanner(){
		dump($_REQUEST);
		dump($_POST);
		if(-1){
			echo "ywe";
		}else{
			echo "nonono";
		}
	}
	//下线
	public function closeAdvert(){
		$aid = $_REQUEST["aid"];
		$AD = M("Advert");
		$advert = $AD->where("aid = $aid")->find();
		if($advert["status"]){
			$AD->is_display = 'n';
			if($AD->where("aid = $aid")->save()){
				$data=array("massage"=>"下线成功","url"=>"partner/Advert/index");
				$this->redirect("partner/Public/redirectHtml",$data);
				exit;
			}else{
				$data=array("massage"=>"下线失败","url"=>"partner/Advert/index");
				$this->redirect("partner/Public/redirectHtml",$data);
				exit;
			}
		}else{
			$data=array("massage"=>"该广告未通过审核","url"=>"partner/Advert/index");
			$this->redirect("partner/Public/redirectHtml",$data);
			exit;
		}
	}
	
	public function editAd(){
// 		dump($_REQUEST);exit;
		$aid = $_REQUEST["aid"];
		$advert = M("Advert")->where("aid=$aid")->find();
// 		dump($aid);
// 		dump($advert);exit;
		$this->assign("advert",$advert);
		$this->display("advert_edit");
	}
	
	public function doEditAd(){
		
		$advert = M("Advert");
		$advert->title = t($_POST["title"]);
		$advert->is_link = t($_POST["is_link"]);
		$advert->link = t($_POST["link"]);
		$advert->content = t($_POST["content"]);	
		$advert->is_banner = t($_REQUEST["is_banner"]);
		$advert->ctime = time();
		$advert->status = 0;
		
		if(isset($_FILES["pic"])){
			if($_FILES["pic"]["error"] == 0){
			$upload= new UploadFile();
			$upload->autoSub=true; //开启子目录保存文件
			$upload->maxSize = 2097152;//最大为2m
			$upload->subName=array('date','Y-m-d');//以时间格式生成文件夹
			$upload->exts=array('jpg','jpeg','gif','png','bmp');//可上传的类型
			$upload->savePath='data/advert/pic/';
			$info=$upload->upload();
			
				
			if(!$info){
				$this->error($upload->getError(),'add',3);
			}
			$avatarinfo = $upload->getUploadFileInfo();
			//dump($avatarinfo);exit;
			$advert->pic=$avatarinfo['0']['savepath'].$avatarinfo['0']['savename'];
			}
		}
		
		
		if($advert->where("aid = ".$_POST["aid"])->save()){
			$data=array("massage"=>"修改并提交成功","url"=>"partner/Advert/index");
			$this->redirect("partner/Public/redirectHtml",$data);
			exit;
		}else{
			$data=array("massage"=>"操作失败".$advert->getDbError()."/".$advert->getLastSql(),"url"=>"partner/Advert/index");
			$this->redirect("partner/Public/redirectHtml",$data);
			exit;
		}
		
	}
	public function delAd(){
		$aid = $_REQUEST["aid"];
		$AD = M("Advert");
			$AD->is_del = 'y';
			if($AD->where("aid = $aid")->save()){
				$data=array("massage"=>"删除成功","url"=>"partner/Advert/index");
				$this->redirect("partner/Public/redirectHtml",$data);
				exit;
			}else{
				$data=array("massage"=>"删除失败","url"=>"partner/Advert/index");
				$this->redirect("partner/Public/redirectHtml",$data);
				exit;
			}	
	}
	
	public function advert_online(){
		$pid = $_SESSION["pid"];
		/*商家信息*/
		$partner = M("Partner")->where("pid = $pid")->find();
		//显示数据
		//可使用
		$adverts = M("Advert")->where("pid = $pid and is_del = 'n'")->field("count(*)")->find()["count(*)"];
		$onlink_advert  = M("Advert")->where("pid = $pid and is_del = 'n' and status = 1")->field("count(*)")->find()["count(*)"];
		//现在情况
		if($adverts == null) $adverts=0;
		if($onlink_advert == null) $onlink_advert=0;
		//点击情况
			
		
		//广告列表
		//通过的
		$advert_pass = M("Advert")->where("pid = $pid and is_del = 'n' and status = 1 and is_display = 'y' ")->select();
		
		if($advert_pass == null) $advert_pass = 0;
		//未通过的
		//$advert_unpass = M("Advert")->where("pid = $pid and is_del = 'n' and (status = 0 or status = -1)")->select();
		$sql = "select * from wz_advert_banner as b left join wz_advert as a on b.aid = a.aid where a.pid = $pid order by b.play_time desc";
		$advert_unpass = M("Advert")->query($sql);
		$this->assign("click_num",$partner["click_num"]);
		$this->assign("click_day",$partner["click_day"]);
		$this->assign("adverts",$adverts);
		$this->assign("onlink_advert",$onlink_advert);
		$this->assign("advert_pass",$advert_pass);
		$this->assign("advert_unpass",$advert_unpass);
		
		
		$this->display("advert_online");
	}
}